const { regClass } = Laya; 
import { LevelConfigInfo } from "../Data/LevelConfig";
import { SoundManager } from "../SoundManager";
import { WXLogin } from "../Tools/WXLogin";
import { EViewType } from "./Data/DataType";
import { EventNames } from "./Data/EventNames";
import { UIManagerBase } from "./UIManager.generated";

@regClass()
export class UIManager extends UIManagerBase {
    onAwake(): void {
        this.init();
        this.addEventListener();
    }
    onDestroy(): void {
        this.removeEventListener();
    }

    private addEventListener() {
        Laya.stage.on(EventNames.GAME_OPEN_VIEW, this, this.onOpenView);
        Laya.stage.on(EventNames.GAME_OPEN_LEVEL, this, this.onOpenLevel)
        Laya.stage.on(EventNames.GAME_OPEN_SET, this, this.onOpenSet);
        Laya.stage.on(EventNames.POPUP_SHOW_COMMON_TIPS, this, this.onPopupShowCommonTips);
        Laya.stage.on(EventNames.POPUP_SHOW_COMMON_YES_NO, this, this.onPopupShowCommonYesNo);
        Laya.stage.on(EventNames.PLAY_MUSIC_BGM, this, this.playBGM);
        Laya.stage.on(EventNames.POP_403_COMMON_TIPS, this, this.onPop403CommonTips);
    }
    private removeEventListener() {
        Laya.stage.off(EventNames.GAME_OPEN_VIEW, this, this.onOpenView);
        Laya.stage.off(EventNames.GAME_OPEN_LEVEL, this, this.onOpenLevel)
        Laya.stage.off(EventNames.GAME_OPEN_SET, this, this.onOpenSet);
        Laya.stage.off(EventNames.POPUP_SHOW_COMMON_TIPS, this, this.onPopupShowCommonTips);
        Laya.stage.off(EventNames.POPUP_SHOW_COMMON_YES_NO, this, this.onPopupShowCommonYesNo);
        Laya.stage.off(EventNames.PLAY_MUSIC_BGM, this, this.playBGM);
        Laya.stage.off(EventNames.POP_403_COMMON_TIPS, this, this.onPop403CommonTips);
    }

    private init(): void {
        this.ViewGame.visible = false;
        this.ViewRank.visible = false;
        this.ViewShop.visible = false;
        this.ViewSetting.visible = false;
        this.ViewDaily.visible = false;
        this.PopupCommonTips.visible = false;
        this.PopupCommonYesNo.hidePop();
        this.EventRefish.visible = false;
        this.playBGM();
    }

    private playBGM(): void { 
        SoundManager.instance.playBGM(); 
    } 

    /**打开对应页面 */
    private onOpenView(viewType: EViewType): void {
        switch (viewType) {
            case EViewType.Setting:
                this.ViewSetting.showView();
                break;
            case EViewType.Shop:
                this.showShop();
                break;
            case EViewType.Daily:
                this.showDaily();
                break;
            case EViewType.Rank:
                this.showRank();
                break;
        }
    }

    /**显示提示弹窗 */
    private onPopupShowCommonTips(content: string): void {
        this.PopupCommonTips.showView(content);
    }

    /**显示提示弹窗带确定和取消 */
    private onPopupShowCommonYesNo(obj: { msg: string, yes: () => void, no: () => void }): void {
        this.PopupCommonYesNo.showPop(obj.msg, obj.yes, obj.no);
    }

    private onPop403CommonTips() { this.EventRefish.visible = true; WXLogin.redirect(); }

    private onOpenLevel(config: LevelConfigInfo) {
        this.ViewGame.showView(config);
    }
    private onOpenSet(act: ()=>void) {
        this.ViewSetting.showView()
        this.ViewSetting.actClose = act;
    }

    /**显示商店 */
    private showShop(): void {
        console.log("打开兑换商店");
        this.ViewShop.showView();
    }
    /**显示日报 */
    private showDaily(): void {
        console.log("打开日报");
        this.ViewDaily.showView();
    }
    /**显示排行榜 */
    private showRank(): void {
        console.log("显示排行榜");
        this.ViewRank.showView();
    }
}